home *** CD-ROM | disk | FTP | other *** search
- Path: inet.dp.beckman.com!newsmaster
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Returning a reference
- Message-ID: <4dp1a7$bo8@phoenix.dse.beckman.com>
- From: jawalker@ccgate.dp.beckman.com (Jack Walker)
- Date: Fri, 19 Jan 1996 21:55:48 GMT
- References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
- <30FA95E1.50C@public.sta.net.cn>
- Distribution: world
- Organization: DDC, Beckman Instruments, Inc.
- NNTP-Posting-Host: j_walker.dse.beckman.com
-
- Xu Ji <jafd@public.sta.net.cn> wrote:
-
- >Martin Aupperle wrote:
- >:
- >: Borland C++ V4.5 does not allow to return a reference to a local
- >: variable:
- >:
- >: int &doIt() {
- >:
- >: int i = 7;
- >: return i; // syntax error
- >: }
- >:
- >: I remember that I once had a compiler that did allow it (it gave me
- >: only a warning).
- >: Which one is right? I think that it should be an error because after
- >: the function has terminated, the reference has no data object it is
- >: bound to any more.
- >:
- >: Martin
- >:
- >: -----------------------------------
- >: Signatures are a waste of bandwidth
- >: -----------------------------------
-
- >I think it should be an error, and I test it under Borland C++
- >and Watcom C++, both this two compiler report it as an error.
-
- >but the following function can be compiled:
-
- > int &doIt()
- > {
- > int i = 7;
- > return (int &)i;
- > }
-
- >Best wishs,
- >Xu yifeng
-
- Do you really want to return a reference to an object that is
- allocated on the stack? After doIt returns the space that i occupied
- is available to be overwritten when the next stack frame is created.
-
-
- Work:jawalker@ccgate.dp.beckman.com
- Home:75341.3260@compuserve.com
-
-